package entities;

import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
public abstract class TreeNode implements Serializable {
    
    private Long id;
    private String name;
    private Calendar dateCreated;
    private Calendar dateModified;
    private TreeNode parent;
    private List<TreeNode> childs;
    
    abstract public String getType();
    
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @NotNull
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Temporal(TemporalType.TIMESTAMP)
    public Calendar getDateCreated() {
        return dateCreated;
    }

    public void setDateCreated(Calendar dateCreated) {
        this.dateCreated = dateCreated;
    }
    
    @Temporal(TemporalType.TIMESTAMP)
    public Calendar getDateModified() {
        return dateModified;
    }

    public void setDateModified(Calendar dateModified) {
        this.dateModified = dateModified;
    }

    @Size(min = 1, max = 25, message = "Name invalid (1-25 characters)")
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @ManyToOne
    public TreeNode getParent() {
        return parent;
    }

    public void setParent(TreeNode parent) {
        if (parent != null && parent.getId() != id) {
            this.parent = parent;
        }
    }

    @OneToMany(mappedBy = "parent", cascade={CascadeType.REMOVE})
    public List<TreeNode> getChilds() {
        return childs;
    }

    public void setChilds(List<TreeNode> childs) {
        this.childs = childs;
    }
}